﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using nRoute.Components;
using nRoute.Navigation;
using System.Runtime.Serialization;
using System.IO;
using System.Text;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using nRoute.Navigation.Containers;
using SimpleShell.Model;

namespace SimpleShell
{
    public partial class MasterShell : UserControl
    {
        private ParametersCollection _state;

        public MasterShell()
        {
            InitializeComponent();

            this.saveState.Click += (s, e) =>
            {
                _state = ((ISupportNavigationViewState)this.shell).SaveState();
            };

            this.restoreState.Click += (s, e) =>
            {
                ((ISupportNavigationViewState)this.shell).RestoreState(_state);
            };

            this.refreshControl.Click += (s, e) =>
            {
                this.LayoutRoot.Children.Remove(this.LayoutRoot.Children.Last());

                var _newShell = new Shell();
                _newShell.SetValue(Grid.RowProperty, 1);

                this.LayoutRoot.Children.Add(_newShell);
                ((ISupportNavigationViewState)_newShell).RestoreState(_state);
            };

            this.serializeState.Click += (s, e) =>
            {
                if (_state != null)
                {
                    var _memStream = new MemoryStream();
                    var _serializer = new XmlSerializer(typeof(ParametersCollection), new Type[] { typeof(Parameter), typeof(PageContentState), 
                        typeof(PageContentState[]), typeof(Object[]), typeof(Object[][]), typeof(Person) });    // note the custom type - Person 
                    _serializer.Serialize(_memStream, _state);

                    var _string = Encoding.UTF8.GetString(_memStream.GetBuffer(), 0, Convert.ToInt32(_memStream.Length));
                    if (IsolatedStorageSettings.ApplicationSettings.Contains("PSTATE"))
                    {
                        IsolatedStorageSettings.ApplicationSettings.Remove("PSTATE");
                    }
                    IsolatedStorageSettings.ApplicationSettings.Add("PSTATE", _string);
                }
            };

            this.deserializeState.Click += (s, e) =>
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains("PSTATE"))
                {
                    var _string = Convert.ToString(IsolatedStorageSettings.ApplicationSettings["PSTATE"]);
                    var _memStream = new MemoryStream(Encoding.UTF8.GetBytes(_string));

                    var _serializer = new XmlSerializer(typeof(ParametersCollection), new Type[] { typeof(Parameter), typeof(PageContentState), 
                        typeof(PageContentState[]), typeof(Object[]), typeof(Object[][]), typeof(Person) }); // note the custom type - Person 
                    _state = (ParametersCollection)_serializer.Deserialize(_memStream);
                }
            };

        }
    }
}
